package edu.unf.cen6940.service;

import edu.unf.cen6940.dao.FacultyresearchtostudentDAO;
import edu.unf.cen6940.dao.StudentDAO;
import edu.unf.cen6940.dao.ThesisDAO;

import edu.unf.cen6940.domain.Facultyresearchtostudent;
import edu.unf.cen6940.domain.Student;
import edu.unf.cen6940.domain.Thesis;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Student entities
 * 
 */

@Service("StudentService")
@Transactional
public class StudentServiceImpl implements StudentService {

	/**
	 * DAO injected by Spring that manages Facultyresearchtostudent entities
	 * 
	 */
	@Autowired
	private FacultyresearchtostudentDAO facultyresearchtostudentDAO;

	/**
	 * DAO injected by Spring that manages Student entities
	 * 
	 */
	@Autowired
	private StudentDAO studentDAO;

	/**
	 * DAO injected by Spring that manages Thesis entities
	 * 
	 */
	@Autowired
	private ThesisDAO thesisDAO;

	/**
	 * Instantiates a new StudentServiceImpl.
	 *
	 */
	public StudentServiceImpl() {
	}

	/**
	 * Return all Student entity
	 * 
	 */
	@Transactional
	public List<Student> findAllStudents(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Student>(studentDAO.findAllStudents(startResult, maxRows));
	}

	/**
	 * Load an existing Student entity
	 * 
	 */
	@Transactional
	public Set<Student> loadStudents() {
		return studentDAO.findAllStudents();
	}

	/**
	 * Save an existing Thesis entity
	 * 
	 */
	@Transactional
	public Student saveStudentThesises(Integer studentId, Thesis related_thesises) {
		Student student = studentDAO.findStudentByPrimaryKey(studentId, -1, -1);
		Thesis existingthesises = thesisDAO.findThesisByPrimaryKey(related_thesises.getThesisId());

		// copy into the existing record to preserve existing relationships
		if (existingthesises != null) {
			existingthesises.setThesisId(related_thesises.getThesisId());
			existingthesises.setTitle(related_thesises.getTitle());
			existingthesises.setAbstract_(related_thesises.getAbstract_());
			existingthesises.setProposalAttempts(related_thesises.getProposalAttempts());
			existingthesises.setDefenseAttempts(related_thesises.getDefenseAttempts());
			existingthesises.setStartDate(related_thesises.getStartDate());
			existingthesises.setDoi(related_thesises.getDoi());
			related_thesises = existingthesises;
		}

		related_thesises.setStudent(student);
		student.getThesises().add(related_thesises);
		related_thesises = thesisDAO.store(related_thesises);
		thesisDAO.flush();

		student = studentDAO.store(student);
		studentDAO.flush();

		return student;
	}

	/**
	 * Delete an existing Thesis entity
	 * 
	 */
	@Transactional
	public Student deleteStudentThesises(Integer student_studentId, Integer related_thesises_thesisId) {
		Thesis related_thesises = thesisDAO.findThesisByPrimaryKey(related_thesises_thesisId, -1, -1);

		Student student = studentDAO.findStudentByPrimaryKey(student_studentId, -1, -1);

		related_thesises.setStudent(null);
		student.getThesises().remove(related_thesises);

		thesisDAO.remove(related_thesises);
		thesisDAO.flush();

		return student;
	}

	/**
	 * Save an existing Facultyresearchtostudent entity
	 * 
	 */
	@Transactional
	public Student saveStudentFacultyresearchtostudents(Integer studentId, Facultyresearchtostudent related_facultyresearchtostudents) {
		Student student = studentDAO.findStudentByPrimaryKey(studentId, -1, -1);
		Facultyresearchtostudent existingfacultyresearchtostudents = facultyresearchtostudentDAO.findFacultyresearchtostudentByPrimaryKey(related_facultyresearchtostudents.getFacultyResearchId(), related_facultyresearchtostudents.getStudentId(), related_facultyresearchtostudents.getFacultyId());

		// copy into the existing record to preserve existing relationships
		if (existingfacultyresearchtostudents != null) {
			existingfacultyresearchtostudents.setFacultyResearchId(related_facultyresearchtostudents.getFacultyResearchId());
			existingfacultyresearchtostudents.setStudentId(related_facultyresearchtostudents.getStudentId());
			existingfacultyresearchtostudents.setFacultyId(related_facultyresearchtostudents.getFacultyId());
			related_facultyresearchtostudents = existingfacultyresearchtostudents;
		}

		related_facultyresearchtostudents.setStudent(student);
		student.getFacultyresearchtostudents().add(related_facultyresearchtostudents);
		related_facultyresearchtostudents = facultyresearchtostudentDAO.store(related_facultyresearchtostudents);
		facultyresearchtostudentDAO.flush();

		student = studentDAO.store(student);
		studentDAO.flush();

		return student;
	}

	/**
	 * Return a count of all Student entity
	 * 
	 */
	@Transactional
	public Integer countStudents() {
		return ((Long) studentDAO.createQuerySingleResult("select count(o) from Student o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing Student entity
	 * 
	 */
	@Transactional
	public void deleteStudent(Student student) {
		studentDAO.remove(student);
		studentDAO.flush();
	}

	/**
	 * Save an existing Student entity
	 * 
	 */
	@Transactional
	public void saveStudent(Student student) {
		Student existingStudent = studentDAO.findStudentByPrimaryKey(student.getStudentId());

		if (existingStudent != null) {
			if (existingStudent != student) {
				existingStudent.setStudentId(student.getStudentId());
				existingStudent.setNnumber(student.getNnumber());
				existingStudent.setFirstName(student.getFirstName());
				existingStudent.setLastName(student.getLastName());
				existingStudent.setEmail(student.getEmail());
				existingStudent.setMajor(student.getMajor());
				existingStudent.setGraduated(student.getGraduated());
				existingStudent.setCurrentPosition(student.getCurrentPosition());
				existingStudent.setYearGraduated(student.getYearGraduated());
			}
			student = studentDAO.store(existingStudent);
		} else {
			student = studentDAO.store(student);
		}
		studentDAO.flush();
	}

	/**
	 * Delete an existing Facultyresearchtostudent entity
	 * 
	 */
	@Transactional
	public Student deleteStudentFacultyresearchtostudents(Integer student_studentId, Integer related_facultyresearchtostudents_facultyResearchId, Integer related_facultyresearchtostudents_studentId, Integer related_facultyresearchtostudents_facultyId) {
		Facultyresearchtostudent related_facultyresearchtostudents = facultyresearchtostudentDAO.findFacultyresearchtostudentByPrimaryKey(related_facultyresearchtostudents_facultyResearchId, related_facultyresearchtostudents_studentId, related_facultyresearchtostudents_facultyId, -1, -1);

		Student student = studentDAO.findStudentByPrimaryKey(student_studentId, -1, -1);

		related_facultyresearchtostudents.setStudent(null);
		student.getFacultyresearchtostudents().remove(related_facultyresearchtostudents);

		facultyresearchtostudentDAO.remove(related_facultyresearchtostudents);
		facultyresearchtostudentDAO.flush();

		return student;
	}

	/**
	 */
	@Transactional
	public Student findStudentByPrimaryKey(Integer studentId) {
		return studentDAO.findStudentByPrimaryKey(studentId);
	}
}
